<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['system:account:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:account:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:account:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:account:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="user_name" align="center" prop="name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:account:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:account:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
    <!-- 添加或修改front_account对话框 -->
    <el-dialog :title="title" v-model="open" width="clamp(350px,90%,600px)" append-to-body>
      <el-form ref="accountRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="user_name" prop="userName">
          <el-input v-model="form.name" placeholder="请输入user_name" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :title="title" v-model="addOpen" width="clamp(350px,90%,600px)" append-to-body>
      <div>表已经存在</div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="yes">确 定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
<script setup name="Account">
import { listAccount, getAccount, delAccount, addAccount, updateAccount } from "@/api/system/account";
import { listTable, getTable, updateTable, addTable,delTable } from '../../../api/system/table'
import _ from 'lodash';
import { ref } from "vue";
const { proxy } = getCurrentInstance();
const accountList = ref([]);
const open = ref(false);
const addOpen = ref(false)
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
let target = ""
const flag = ref(false)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null, userName: null,
  },
  rules: {
  }
});
const { queryParams, form, rules } = toRefs(data);
/** 查询su列表 */
function getList() {
  loading.value = true;
  listTable(queryParams.value).then(response => {
    accountList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    // console.log('data:', response);

  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    name: null
  };
  proxy.resetForm("accountRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加单词表";
  target = ""
  flag.value = true
}
/** 修改按钮操作 */
function handleUpdate(row) {
  flag.value = false
  reset();
  // console.log(row);

  const name = row.name || ids.value;
  getTable(name).then(response => {
    // console.log(response);

    form.value = response.data;
    target = _.cloneDeep(response.data);
    open.value = true;
    title.value = "修改表名";
  });
}
/** 提交按钮 */
function submitForm() {

  if (!flag.value) {
    updateTable(target.name, form.value.name).then(resp => {
      // console.log(resp);
      open.value = false;
      getList();
    })
  } else {
    const system = ['front_account', 'gen_table', 'gen_table_column', 'sys_config', 'sys_dept', 'sys_dict_data', 'sys_dict_type', 'sys_logininfor', 'sys_menu', 'sys_notice', 'sys_post', 'sys_role', 'sys_role_dept', 'sys_role_menu', 'sys_user', 'sys_user_post', 'sys_user_role',]
    const tableList = accountList.value.map(e => e.name)
    const all = system.concat(tableList)
    if (!all.includes(form.value.name)) {
      addTable(form.value.name).then(res => {
        console.log(res);
        open.value = false;
        getList();
      })
    } else {
      addOpen.value = true
    }

  }

}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除"' + row.name + '"的数据项？').then(function () {
    return delTable( row.name);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
  // proxy.download('system/account/export', {
  //   ...queryParams.value
  // }, `account_${new Date().getTime()}.xlsx`)
}

// 添加模态框确认
function yes() {
  addOpen.value = false
}



getList();
</script>
